package com.jiangjiesheng.keepappalive;

import android.content.Context;
import android.content.Intent;

import com.jiangjiesheng.keepappalive.receiver.ScreenReceiverUtil;
import com.jiangjiesheng.keepappalive.service.DaemonService;
import com.jiangjiesheng.keepappalive.service.PlayerMusicService;
import com.jiangjiesheng.keepappalive.utils.HwPushManager;
import com.jiangjiesheng.keepappalive.utils.JobSchedulerManager;
import com.jiangjiesheng.keepappalive.utils.ScreenManager;

/**
 * KeepAliveReceiver好像没有在这里别管理，手动绑定广播可能导致 不能监听重启，那就使用标志位来控制一下
 */

public class KeepAliveManager {
    private static Context context;
    private static KeepAliveManagerStatusListener listener;

    private static volatile KeepAliveManager instance = null;

    private KeepAliveManager() {
    }

    public static KeepAliveManager getInstance(Context context) {
        KeepAliveManager.context = context;
        if (null == instance) {
            synchronized (KeepAliveManager.class) {
                if (null == instance) { //1
                    instance = new KeepAliveManager();
                }
            }
        }
        return instance;
    }

    public void init() {//onCreate 方法中调用
        init(null);
    }

    public void init(KeepAliveManagerStatusListener listener) {
        KeepAliveManager.listener = listener;

        // 1. 注册锁屏广播监听器
        mScreenListener = new ScreenReceiverUtil(context);
        mScreenManager = ScreenManager.getScreenManagerInstance(context);
        mScreenListener.setScreenReceiverListener(mScreenListenerer);
        // 2. 启动系统任务
        if (!JobSchedulerManager.isBelowLOLLIPOP()) {
            mJobManager = JobSchedulerManager.getJobSchedulerInstance(context);
            mJobManager.startJobScheduler();
        }
        // 3. 华为推送保活，允许接收透传
        mHwPushManager = HwPushManager.getInstance(context);
        mHwPushManager.startRequestToken();
        mHwPushManager.isEnableReceiveNormalMsg(true);
        mHwPushManager.isEnableReceiverNotifyMsg(true);
    }

    /**
     * 开启保活
     */
    public void startKeeper() {
        // 3. 启动前台Service
        startDaemonService();
        // 4. 启动播放音乐Service
        startPlayMusicService();
    }

    /**
     * 关闭保活
     */
    public void stopKeeper() {
        //关闭前台Service
        stopDaemonService();
        //关闭启动播放音乐Service
        stopPlayMusicService();
    }

    /**
     * 这个在Activity中的onDestroy方法中调用
     */
    public void stopScreenListenerOnDestroy() {
        if (mScreenListener != null) {
            mScreenListener.stopScreenReceiverListener();
        }
    }

    private void stopPlayMusicService() {
        Intent intent = new Intent(context, PlayerMusicService.class);
        context.stopService(intent);
    }

    private void startPlayMusicService() {
        Intent intent = new Intent(context, PlayerMusicService.class);
        context.startService(intent);//portsActivity.this
    }

    private void startDaemonService() {
        Intent intent = new Intent(context, DaemonService.class);//SportsActivity.this
        context.startService(intent);
    }

    private void stopDaemonService() {
        Intent intent = new Intent(context, DaemonService.class);
        context.stopService(intent);
    }


    // 动态注册锁屏等广播
    private ScreenReceiverUtil mScreenListener;
    // 1像素Activity管理类
    private ScreenManager mScreenManager;
    // JobService，执行系统任务
    private JobSchedulerManager mJobManager;
    // 华为推送管理类
    private HwPushManager mHwPushManager;

    private ScreenReceiverUtil.SreenStateListener mScreenListenerer = new ScreenReceiverUtil.SreenStateListener() {
        @Override
        public void onSreenOn() {
            // 亮屏，移除"1像素"
            mScreenManager.finishActivity();
        }

        @Override
        public void onSreenOff() {
            // 接到锁屏广播，将SportsActivity切换到可见模式
            // "咕咚"、"乐动力"、"悦动圈"就是这么做滴
//            Intent intent = new Intent(SportsActivity.this,SportsActivity.class);
//            startActivity(intent);
            // 如果你觉得，直接跳出SportActivity很不爽
            // 那么，我们就制造个"1像素"惨案
            mScreenManager.startActivity();
        }

        @Override
        public void onUserPresent() {
            // 解锁，暂不用，保留
            //通过接口接口回调 让别人实现
            if (listener != null) {
                listener.onUserPresent();
            }
        }
    };

    public interface KeepAliveManagerStatusListener {
        /**
         * 用户解锁
         */
        void onUserPresent();
    }
}
